package com.yy.bolg.controller.hik;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.entity.CustomerBean;
import com.yy.bolg.entity.FeedbackBean;
import com.yy.bolg.entity.FixedPointBean;
import com.yy.bolg.request.CustomerRequest;
import com.yy.bolg.request.FixedPointRequest;
import com.yy.bolg.request.hik.DeviceToSensorRequest;
import com.yy.bolg.request.hik.DirectConnectionRequest;
import com.yy.bolg.request.hik.HistoryMonitorRequest;
import com.yy.bolg.resp.*;
import com.yy.bolg.resp.hik.HikFireCloudDeviceResp;
import com.yy.bolg.resp.hik.HikFireCloudHistoryMonitorResp;
import com.yy.bolg.resp.hik.HikFireCloudSensorResp;
import com.yy.bolg.service.CustomerService;
import com.yy.bolg.service.FixedPointService;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import com.yy.bolg.util.hikfirecloud.HikFireCloud;
import com.yy.bolg.util.hikfirecloud.HikFireCloudDictionaries;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 海康相关控制器
 * @author enmaoFu
 * @date 2024-12-13
 * RequestMapping("/hik") 是说明这个类的访问地址是 /hik
 */
@Api(tags = {"海康相关接口（业务平台调用）"})
@Controller
@RequestMapping(value = "/hik")
public class BusinessController {

    @Autowired
    private CustomerService customerService;// 自动载入Service对象

    @Autowired
    private FixedPointService fixedPointService;// 自动载入Service对象

    /**
     * 根据单位编号查询所有传感器
     * 服务器访问路径：http://localhost:8080/bolg/hik/business/querySensorList
     * 本地访问路径：http://localhost:8111/hik/business/querySensorList
     * 参数：{"customerCode":"CUS20241102102456","pageNum":1,"pageSize":10}
     */
    @ApiOperation(value = "根据单位编号查询所有传感器", notes = "参数：{\"customerCode\":\"CUS20241102102456\",\"pageNum\":1,\"pageSize\":10}", response = SensorResp.class)
    @RequestMapping(value = "/business/querySensorList",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String querySensorList(@RequestBody CustomerRequest request){
        String result;

        CustomerBean customerBean = new CustomerBean();
        customerBean.setPsCustomerCode(request.getCustomerCode());

        try {
            CustomerBean returnCustomerBean = customerService.findOneByCode(customerBean);

            HikFireCloud hikFireCloud = new HikFireCloud();

            Map<String, String> getSensorListParamMap = new HashMap<>();
            getSensorListParamMap.put("flagId", "");
            getSensorListParamMap.put("offset", "0");
            getSensorListParamMap.put("unitID", returnCustomerBean.getPsCustomerUnitid());
            getSensorListParamMap.put("pageNo", String.valueOf(request.getPageNum()));
            getSensorListParamMap.put("pageSize", String.valueOf(request.getPageSize()));

            JSONObject sensorListJson = JSONObject.parseObject(hikFireCloud.getSensorList(getSensorListParamMap));
            JSONObject sensorListDataJson = JSON.parseObject(sensorListJson.get("data").toString());
            List<HikFireCloudSensorResp.Sensor> hikFireCloudSensorResps = JSON.parseArray(sensorListDataJson.get("list").toString(), HikFireCloudSensorResp.Sensor.class);

            List<SensorResp> sensorRespList = new ArrayList<>();
            for(HikFireCloudSensorResp.Sensor sensor: hikFireCloudSensorResps){
                SensorResp sr = new SensorResp();
                sr.setAreaID(sensor.getAreaID());
                sr.setCompanyID(sensor.getCompanyID());
                sr.setCreateTime(sensor.getCreateTime());
                sr.setDeviceID(sensor.getDeviceID());
                sr.setLocation(sensor.getLocation());
                sr.setMaxThresholdValue(sensor.getMaxThresholdValue());
                sr.setMinThresholdValue(sensor.getMinThresholdValue());
                sr.setSensorID(sensor.getSensorID());
                sr.setSensorName(sensor.getSensorName());
                sr.setSensorSerial(sensor.getSensorSerial());
                sr.setSensorType(sensor.getSensorType());
                sr.setSystemType(sensor.getSystemType());
                sr.setUnitID(sensor.getUnitID());
                sr.setUpdateTime(sensor.getUpdateTime());
                sensorRespList.add(sr);
            }

            Page page = new Page();
            page.setTotal(Integer.parseInt(sensorListDataJson.get("total").toString()));
            page.setPageSize(Integer.parseInt(sensorListDataJson.get("pageSize").toString()));
            page.setPageNum(Integer.parseInt(sensorListDataJson.get("pageNo").toString()));
            page.setPages(Integer.parseInt(sensorListDataJson.get("totalPage").toString()));

            Map map = new HashMap();
            map.put("pageInfo", page);
            map.put("rows", sensorRespList);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据设备ID查询所有传感器
     * 服务器访问路径：http://localhost:8080/bolg/hik/business/querySensorToDeviceIdList
     * 本地访问路径：http://localhost:8111/hik/business/querySensorToDeviceIdList
     * 参数：{"deviceID":"1065998578609803264","pageNum":1,"pageSize":10}
     */
    @ApiOperation(value = "根据设备ID查询所有传感器", notes = "参数：{\"deviceID\":\"1065998578609803264\",\"pageNum\":1,\"pageSize\":10}", response = SensorResp.class)
    @RequestMapping(value = "/business/querySensorToDeviceIdList",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String querySensorToDeviceIdList(@RequestBody DeviceToSensorRequest request){
        String result;

        try {
            HikFireCloud hikFireCloud = new HikFireCloud();

            Map<String, String> getSensorListParamMap = new HashMap<>();
            getSensorListParamMap.put("flagId", "");
            getSensorListParamMap.put("offset", "0");
            getSensorListParamMap.put("deviceID", request.getDeviceID());
            getSensorListParamMap.put("pageNo", String.valueOf(request.getPageNum()));
            getSensorListParamMap.put("pageSize", String.valueOf(request.getPageSize()));

            JSONObject sensorListJson = JSONObject.parseObject(hikFireCloud.getSensorList(getSensorListParamMap));
            JSONObject sensorListDataJson = JSON.parseObject(sensorListJson.get("data").toString());
            List<HikFireCloudSensorResp.Sensor> hikFireCloudSensorResps = JSON.parseArray(sensorListDataJson.get("list").toString(), HikFireCloudSensorResp.Sensor.class);

            List<SensorResp> sensorRespList = new ArrayList<>();
            for(HikFireCloudSensorResp.Sensor sensor: hikFireCloudSensorResps){
                SensorResp sr = new SensorResp();
                sr.setAreaID(sensor.getAreaID());
                sr.setCompanyID(sensor.getCompanyID());
                sr.setCreateTime(sensor.getCreateTime());
                sr.setDeviceID(sensor.getDeviceID());
                sr.setLocation(sensor.getLocation());
                sr.setMaxThresholdValue(sensor.getMaxThresholdValue());
                sr.setMinThresholdValue(sensor.getMinThresholdValue());
                sr.setSensorID(sensor.getSensorID());
                sr.setSensorName(sensor.getSensorName());
                sr.setSensorSerial(sensor.getSensorSerial());
                sr.setSensorType(sensor.getSensorType());
                sr.setSystemType(sensor.getSystemType());
                sr.setUnitID(sensor.getUnitID());
                sr.setUpdateTime(sensor.getUpdateTime());
                sensorRespList.add(sr);
            }

            Page page = new Page();
            page.setTotal(Integer.parseInt(sensorListDataJson.get("total").toString()));
            page.setPageSize(Integer.parseInt(sensorListDataJson.get("pageSize").toString()));
            page.setPageNum(Integer.parseInt(sensorListDataJson.get("pageNo").toString()));
            page.setPages(Integer.parseInt(sensorListDataJson.get("totalPage").toString()));

            Map map = new HashMap();
            map.put("pageInfo", page);
            map.put("rows", sensorRespList);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据传感器ID查询对应传感器
     * 服务器访问路径：http://localhost:8080/bolg/hik/business/querySensorToSensorID
     * 本地访问路径：http://localhost:8111/hik/business/querySensorToSensorID
     * 参数：{"sensorID":"1065998578609803264","pageNum":1,"pageSize":10}
     */
    @ApiOperation(value = "根据设备ID查询所有传感器", notes = "参数：{\"sensorID\":\"1065998578609803264\",\"pageNum\":1,\"pageSize\":10}", response = SensorResp.class)
    @RequestMapping(value = "/business/querySensorToSensorID",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String querySensorToSensorID(@RequestBody DeviceToSensorRequest request){
        String result;

        try {
            HikFireCloud hikFireCloud = new HikFireCloud();

            Map<String, String> getSensorListParamMap = new HashMap<>();
            getSensorListParamMap.put("flagId", "");
            getSensorListParamMap.put("offset", "0");
            getSensorListParamMap.put("sensorID", request.getSensorID());
            getSensorListParamMap.put("pageNo", String.valueOf(request.getPageNum()));
            getSensorListParamMap.put("pageSize", String.valueOf(request.getPageSize()));

            JSONObject sensorListJson = JSONObject.parseObject(hikFireCloud.getSensorList(getSensorListParamMap));
            JSONObject sensorListDataJson = JSON.parseObject(sensorListJson.get("data").toString());
            List<HikFireCloudSensorResp.Sensor> hikFireCloudSensorResps = JSON.parseArray(sensorListDataJson.get("list").toString(), HikFireCloudSensorResp.Sensor.class);

            SensorResp sr = new SensorResp();
            if(null != hikFireCloudSensorResps && hikFireCloudSensorResps.size() != 0){
                sr.setAreaID(hikFireCloudSensorResps.get(0).getAreaID());
                sr.setCompanyID(hikFireCloudSensorResps.get(0).getCompanyID());
                sr.setCreateTime(hikFireCloudSensorResps.get(0).getCreateTime());
                sr.setDeviceID(hikFireCloudSensorResps.get(0).getDeviceID());
                sr.setLocation(hikFireCloudSensorResps.get(0).getLocation());
                sr.setMaxThresholdValue(hikFireCloudSensorResps.get(0).getMaxThresholdValue());
                sr.setMinThresholdValue(hikFireCloudSensorResps.get(0).getMinThresholdValue());
                sr.setSensorID(hikFireCloudSensorResps.get(0).getSensorID());
                sr.setSensorName(hikFireCloudSensorResps.get(0).getSensorName());
                sr.setSensorSerial(hikFireCloudSensorResps.get(0).getSensorSerial());
                sr.setSensorType(hikFireCloudSensorResps.get(0).getSensorType());
                sr.setSystemType(hikFireCloudSensorResps.get(0).getSystemType());
                sr.setUnitID(hikFireCloudSensorResps.get(0).getUnitID());
                sr.setUpdateTime(hikFireCloudSensorResps.get(0).getUpdateTime());
            }
            Map map = new HashMap();
            map.put("data", sr);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据单位编号查询所有设备
     * 服务器访问路径：http://localhost:8080/bolg/hik/business/queryDeviceList
     * 本地访问路径：http://localhost:8111/hik/business/queryDeviceList
     * 参数：{"customerCode":"CUS20241219000133","deviceSerial":"FR8589391","pageNum":1,"pageSize":10}
     */
    @ApiOperation(value = "根据单位编号查询所有设备", notes = "参数（设备序列号可为空）：{\"customerCode\":\"CUS20241219000133\",\"deviceSerial\":\"FR8589391\",\"pageNum\":1,\"pageSize\":10}", response = DeviceResp.class)
    @RequestMapping(value = "/business/queryDeviceList",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String queryDeviceList(@RequestBody DeviceToSensorRequest request){
        String result;

        CustomerBean customerBean = new CustomerBean();
        customerBean.setPsCustomerCode(request.getCustomerCode());

        try {
            CustomerBean returnCustomerBean = customerService.findOneByCode(customerBean);

            if(null != returnCustomerBean){
                HikFireCloud hikFireCloud = new HikFireCloud();

                Map<String, String> getDeviceListParamMap = new HashMap<>();
                getDeviceListParamMap.put("flagId", "");
                getDeviceListParamMap.put("offset", "0");
                getDeviceListParamMap.put("deviceSerial", request.getDeviceSerial());
                getDeviceListParamMap.put("unitID", returnCustomerBean.getPsCustomerUnitid());
                getDeviceListParamMap.put("pageNo", String.valueOf(request.getPageNum()));
                getDeviceListParamMap.put("pageSize", String.valueOf(request.getPageSize()));

                JSONObject deviceListJson = JSONObject.parseObject(hikFireCloud.getDeviceList(getDeviceListParamMap));
                JSONObject deviceListDataJson = JSON.parseObject(deviceListJson.get("data").toString());
                List<HikFireCloudDeviceResp.Device> hikFireCloudDeviceResps = JSON.parseArray(deviceListDataJson.get("list").toString(), HikFireCloudDeviceResp.Device.class);

                List<DeviceResp> deviceRespRespList = new ArrayList<>();
                for(HikFireCloudDeviceResp.Device device: hikFireCloudDeviceResps){
                    DeviceResp dr = new DeviceResp();
                    dr.setAreaID(device.getAreaID());
                    dr.setCommunicationType(device.getCommunicationType());
                    dr.setCompanyID(device.getCompanyID());
                    dr.setCreateTime(device.getCreateTime());
                    dr.setDeviceID(device.getDeviceID());
                    dr.setDeviceModel(HikFireCloudDictionaries.getDeviceModelStr(device.getDeviceModel()));
                    dr.setDeviceName(device.getDeviceName());
                    dr.setDeviceSerial(device.getDeviceSerial());
                    dr.setDeviceStatus(HikFireCloudDictionaries.getDeviceStateStr(device.getDeviceStatus()));
                    dr.setDeviceType(HikFireCloudDictionaries.getDeviceTypeStr(device.getDeviceType()));
                    dr.setLocation(device.getLocation());
                    dr.setMaxThresholdValue(device.getMaxThresholdValue());
                    dr.setMinThresholdValue(device.getMinThresholdValue());
                    dr.setProducterID(device.getProducterID());
                    dr.setSystemType(device.getSystemType());
                    dr.setUnitID(device.getUnitID());
                    dr.setUpdateTime(device.getUpdateTime());
                    dr.setLon(device.getLon());
                    dr.setLat(device.getLat());
                    dr.setUnitStatus(HikFireCloudDictionaries.getUnitStateStr(device.getUnitStatus()));
                    deviceRespRespList.add(dr);
                }

                Page page = new Page();
                page.setTotal(Integer.parseInt(deviceListDataJson.get("total").toString()));
                page.setPageSize(Integer.parseInt(deviceListDataJson.get("pageSize").toString()));
                page.setPageNum(Integer.parseInt(deviceListDataJson.get("pageNo").toString()));
                page.setPages(Integer.parseInt(deviceListDataJson.get("totalPage").toString()));

                Map map = new HashMap();
                map.put("pageInfo", page);
                map.put("rows", deviceRespRespList);

                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
            }else{
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "没有该业主单位",new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据设备ID或传感器ID查询历史监测数据
     * 服务器访问路径：http://localhost:8080/bolg/hik/business/queryDeviceToSensorHistoryMonitorList
     * 本地访问路径：http://localhost:8111/hik/business/queryDeviceToSensorHistoryMonitorList
     * 参数：{"sensorID":"1065998578681106434","deviceID":"1065998578609803264","startTime":"2024-12-16T00:00:00.000+08:00","endTime":"2024-12-16T23:59:59.000+08:00","pageNum":1,"pageSize":10}
     */
    @ApiOperation(value = "根据设备ID或传感器ID查询历史监测数据", notes = "参数（传感器ID和设备ID可为空字符串，查谁就传谁的ID，不可同时传传感器ID和设备ID）：{\"sensorID\":\"1065998578681106434\",\"deviceID\":\"1065998578609803264\",\"startTime\":\"2024-12-16T00:00:00.000+08:00\",\"endTime\":\"2024-12-16T23:59:59.000+08:00\",\"pageNum\":1,\"pageSize\":10}", response = HistoryMonitorResp.class)
    @RequestMapping(value = "/business/queryDeviceToSensorHistoryMonitorList",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String queryDeviceToSensorHistoryMonitorList(@RequestBody HistoryMonitorRequest request){
        String result;

        try {
            HikFireCloud hikFireCloud = new HikFireCloud();

            Map<String, String> getHistoryMonitorListParamMap = new HashMap<>();
            getHistoryMonitorListParamMap.put("sensorID",request.getSensorID());
            getHistoryMonitorListParamMap.put("deviceID",request.getDeviceID());
            getHistoryMonitorListParamMap.put("flagId", "");
            getHistoryMonitorListParamMap.put("monitorStartTime", request.getStartTime());
            getHistoryMonitorListParamMap.put("monitorStopTime", request.getEndTime());
            getHistoryMonitorListParamMap.put("offset", "0");
            getHistoryMonitorListParamMap.put("pageNo", String.valueOf(request.getPageNum()));
            getHistoryMonitorListParamMap.put("pageSize", String.valueOf(request.getPageSize()));

            String s=hikFireCloud.getHistoryMonitor(getHistoryMonitorListParamMap);
            System.out.println(s);
            JSONObject historyMonitorListJson = JSONObject.parseObject(s);
            JSONObject historyMonitorListDataJson = JSON.parseObject(historyMonitorListJson.get("data").toString());
            List<HikFireCloudHistoryMonitorResp.HistoryMonitor> hikFireCloudHistoryMonitorResps = JSON.parseArray(historyMonitorListDataJson.get("list").toString(), HikFireCloudHistoryMonitorResp.HistoryMonitor.class);

            HistoryMonitorResp historyMonitorResp = new HistoryMonitorResp();
            if(null != hikFireCloudHistoryMonitorResps && hikFireCloudHistoryMonitorResps.size() != 0) {
                historyMonitorResp.setCompanyID(hikFireCloudHistoryMonitorResps.get(0).getCompanyID());
                historyMonitorResp.setResourceID(hikFireCloudHistoryMonitorResps.get(0).getResourceID());
                historyMonitorResp.setResourceSerial(hikFireCloudHistoryMonitorResps.get(0).getResourceSerial());
                historyMonitorResp.setUnitName(hikFireCloudHistoryMonitorResps.get(0).getUnitName());
                historyMonitorResp.setCompanyName(hikFireCloudHistoryMonitorResps.get(0).getCompanyName());
                historyMonitorResp.setSystemType(hikFireCloudHistoryMonitorResps.get(0).getSystemType());
                historyMonitorResp.setUnitID(hikFireCloudHistoryMonitorResps.get(0).getUnitID());
                historyMonitorResp.setResourceName(hikFireCloudHistoryMonitorResps.get(0).getResourceName());
                historyMonitorResp.setType(hikFireCloudHistoryMonitorResps.get(0).getType());
                historyMonitorResp.setResourceType(hikFireCloudHistoryMonitorResps.get(0).getResourceType());
                historyMonitorResp.setAreaID(hikFireCloudHistoryMonitorResps.get(0).getAreaID());
                historyMonitorResp.setAreaName(hikFireCloudHistoryMonitorResps.get(0).getAreaName());
                historyMonitorResp.setMonitorTime(hikFireCloudHistoryMonitorResps.get(0).getMonitorTime().substring(0,10) + " " + hikFireCloudHistoryMonitorResps.get(0).getMonitorTime().substring(11,19));

                List<HistoryMonitorResp.HistoryMonitorDataResp> historyMonitorDataResps = new ArrayList<>();
                for (HikFireCloudHistoryMonitorResp.HistoryMonitor historyMonitor : hikFireCloudHistoryMonitorResps) {
                    for (HikFireCloudHistoryMonitorResp.HistoryMonitor.HistoryMonitorData historyMonitorData : historyMonitor.getData()) {
                        HistoryMonitorResp.HistoryMonitorDataResp historyMonitorDataResp = new HistoryMonitorResp.HistoryMonitorDataResp();
                        historyMonitorDataResp.setMonitorType(HikFireCloudDictionaries.getMonitorTypeStr(historyMonitorData.getMonitorType()));
                        historyMonitorDataResp.setResourceID(historyMonitorData.getResourceID());
                        historyMonitorDataResp.setResourceSerial(historyMonitorData.getResourceSerial());
                        historyMonitorDataResp.setMonitorUnit(historyMonitorData.getMonitorUnit());
                        historyMonitorDataResp.setMonitorValue(historyMonitorData.getMonitorValue());
                        historyMonitorDataResp.setType(historyMonitorData.getType());
                        historyMonitorDataResp.setMonitorState(HikFireCloudDictionaries.getMonitorStateStr(historyMonitorData.getMonitorState()));
                        historyMonitorDataResp.setMonitorTime(historyMonitor.getMonitorTime());
                        historyMonitorDataResp.setSensorType(HikFireCloudDictionaries.getSensorTypeStr(historyMonitorData.getResourceType()));
                        historyMonitorDataResps.add(historyMonitorDataResp);
                    }
                    historyMonitorResp.setHistoryMonitorDataList(historyMonitorDataResps);
                }
            }

            Page page = new Page();
            page.setTotal(Integer.parseInt(historyMonitorListDataJson.get("total").toString()));
            page.setPageSize(Integer.parseInt(historyMonitorListDataJson.get("pageSize").toString()));
            page.setPageNum(Integer.parseInt(historyMonitorListDataJson.get("pageNo").toString()));
            page.setPages(Integer.parseInt(historyMonitorListDataJson.get("totalPage").toString()));

            Map map = new HashMap();
            map.put("pageInfo", page);
            map.put("historyMonitor", historyMonitorResp);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据设备ID或传感器ID查询监测数据
     * 服务器访问路径：http://localhost:8080/bolg/hik/business/queryDeviceToSensorMonitorList
     * 本地访问路径：http://localhost:8111/hik/business/queryDeviceToSensorMonitorList
     * 参数：{"sensorID":"1065998578681106434","deviceID":"1065998578609803264","startTime":"2024-12-16T00:00:00.000+08:00","endTime":"2024-12-16T23:59:59.000+08:00","pageNum":1,"pageSize":10}
     */
    @ApiOperation(value = "根据设备ID或传感器ID查询监测数据", notes = "参数（传感器ID和设备ID可为空字符串，查谁就传谁的ID，不可同时传传感器ID和设备ID）：{\"sensorID\":\"1065998578681106434\",\"deviceID\":\"1065998578609803264\",\"startTime\":\"2024-12-16T00:00:00.000+08:00\",\"endTime\":\"2024-12-16T23:59:59.000+08:00\",\"pageNum\":1,\"pageSize\":10}", response = HistoryMonitorResp.class)
    @RequestMapping(value = "/business/queryDeviceToSensorMonitorList",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String queryDeviceToSensorMonitorList(@RequestBody HistoryMonitorRequest request){
        String result;

        try {
            HikFireCloud hikFireCloud = new HikFireCloud();

            Map<String, String> getMonitorListParamMap = new HashMap<>();
            getMonitorListParamMap.put("sensorID",request.getSensorID());
            getMonitorListParamMap.put("deviceID",request.getDeviceID());
            getMonitorListParamMap.put("flagId", "");
            getMonitorListParamMap.put("monitorStartTime", request.getStartTime());
            getMonitorListParamMap.put("monitorStopTime", request.getEndTime());
            getMonitorListParamMap.put("offset", "0");
            getMonitorListParamMap.put("pageNo", String.valueOf(request.getPageNum()));
            getMonitorListParamMap.put("pageSize", String.valueOf(request.getPageSize()));

            JSONObject historyMonitorListJson = JSONObject.parseObject(hikFireCloud.getMonitor(getMonitorListParamMap));
            JSONObject historyMonitorListDataJson = JSON.parseObject(historyMonitorListJson.get("data").toString());
            List<HikFireCloudHistoryMonitorResp.HistoryMonitor> hikFireCloudHistoryMonitorResps = JSON.parseArray(historyMonitorListDataJson.get("list").toString(), HikFireCloudHistoryMonitorResp.HistoryMonitor.class);

            HistoryMonitorResp historyMonitorResp = new HistoryMonitorResp();
            if(null != hikFireCloudHistoryMonitorResps && hikFireCloudHistoryMonitorResps.size() != 0){
                historyMonitorResp.setCompanyID(hikFireCloudHistoryMonitorResps.get(0).getCompanyID());
                historyMonitorResp.setResourceID(hikFireCloudHistoryMonitorResps.get(0).getResourceID());
                historyMonitorResp.setResourceSerial(hikFireCloudHistoryMonitorResps.get(0).getResourceSerial());
                historyMonitorResp.setUnitName(hikFireCloudHistoryMonitorResps.get(0).getUnitName());
                historyMonitorResp.setCompanyName(hikFireCloudHistoryMonitorResps.get(0).getCompanyName());
                historyMonitorResp.setSystemType(hikFireCloudHistoryMonitorResps.get(0).getSystemType());
                historyMonitorResp.setUnitID(hikFireCloudHistoryMonitorResps.get(0).getUnitID());
                historyMonitorResp.setResourceName(hikFireCloudHistoryMonitorResps.get(0).getResourceName());
                historyMonitorResp.setType(hikFireCloudHistoryMonitorResps.get(0).getType());
                historyMonitorResp.setResourceType(hikFireCloudHistoryMonitorResps.get(0).getResourceType());
                historyMonitorResp.setAreaID(hikFireCloudHistoryMonitorResps.get(0).getAreaID());
                historyMonitorResp.setAreaName(hikFireCloudHistoryMonitorResps.get(0).getAreaName());
                historyMonitorResp.setMonitorTime(hikFireCloudHistoryMonitorResps.get(0).getMonitorTime().substring(0,10) + " " + hikFireCloudHistoryMonitorResps.get(0).getMonitorTime().substring(11,19));

                for(HikFireCloudHistoryMonitorResp.HistoryMonitor historyMonitor: hikFireCloudHistoryMonitorResps){
                    List<HistoryMonitorResp.HistoryMonitorDataResp> historyMonitorDataResps = new ArrayList<>();
                    for(HikFireCloudHistoryMonitorResp.HistoryMonitor.HistoryMonitorData historyMonitorData: historyMonitor.getData()){
                        if(historyMonitorData.getType() == 2){
                            HistoryMonitorResp.HistoryMonitorDataResp historyMonitorDataResp = new HistoryMonitorResp.HistoryMonitorDataResp();
                            historyMonitorDataResp.setMonitorType(HikFireCloudDictionaries.getMonitorTypeStr(historyMonitorData.getMonitorType()));
                            historyMonitorDataResp.setResourceID(historyMonitorData.getResourceID());
                            historyMonitorDataResp.setResourceSerial(historyMonitorData.getResourceSerial());
                            historyMonitorDataResp.setMonitorUnit(historyMonitorData.getMonitorUnit());
                            historyMonitorDataResp.setMonitorValue(historyMonitorData.getMonitorValue());
                            historyMonitorDataResp.setType(historyMonitorData.getType());
                            historyMonitorDataResp.setMonitorState(HikFireCloudDictionaries.getMonitorStateStr(historyMonitorData.getMonitorState()));
                            historyMonitorDataResp.setMonitorTime(historyMonitor.getMonitorTime());
                            historyMonitorDataResp.setSensorType(HikFireCloudDictionaries.getSensorTypeStr(historyMonitorData.getResourceType()));
                            historyMonitorDataResps.add(historyMonitorDataResp);
                        }
                    }
                    historyMonitorResp.setHistoryMonitorDataList(historyMonitorDataResps);
                }
            }
            Page page = new Page();
            page.setTotal(Integer.parseInt(historyMonitorListDataJson.get("total").toString()));
            page.setPageSize(Integer.parseInt(historyMonitorListDataJson.get("pageSize").toString()));
            page.setPageNum(Integer.parseInt(historyMonitorListDataJson.get("pageNo").toString()));
            page.setPages(Integer.parseInt(historyMonitorListDataJson.get("totalPage").toString()));

            Map map = new HashMap();
            map.put("pageInfo", page);
            map.put("historyMonitor", historyMonitorResp);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 动态请求海康
     * 服务器访问路径：http://localhost:8080/bolg/hik/business/directConnectionHik
     * 本地访问路径：http://localhost:8111/hik/business/directConnectionHik
     * 参数：{"url":"/api/device/v2/getDevice","map":"{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}"}
     */
    @ApiOperation(value = "动态请求海康", notes = "参数：{\"url\":\"/api/device/v2/getDevice\",\"map\":\"{\\\"0\\\":\\\"zhangsan\\\",\\\"1\\\":\\\"lisi\\\",\\\"2\\\":\\\"wangwu\\\",\\\"3\\\":\\\"maliu\\\"}\"}", response = HistoryMonitorResp.class)
    @RequestMapping(value = "/business/directConnectionHik",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String directConnectionHik(@RequestBody DirectConnectionRequest request){
        String result;

        try {
            Map mapTypes = JSON.parseObject(request.getMap());
            System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");
            for (Object obj : mapTypes.keySet()){
                System.out.println("key为："+obj+"值为："+mapTypes.get(obj));
            }

            HikFireCloud hikFireCloud = new HikFireCloud();
            JSONObject historyMonitorListJson = JSONObject.parseObject(hikFireCloud.directConnectionHik(request.getUrl(),mapTypes));

            Map map = new HashMap();
            map.put("hikJson", historyMonitorListJson.toJSONString());

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据设备ID查询所有00点数据
     * 服务器访问路径：http://localhost:8080/bolg/hik/business/queryFixedPointList
     * 本地访问路径：http://localhost:8111/hik/business/queryFixedPointList
     * 参数：{"deviceId":"1114188451560415232","startTime":"2025-01-01","endTime":"2025-04-12"}
     */
    @ApiOperation(value = "根据设备ID和起始日期查询所有00点数据", notes = "参数：{\"deviceId\":\"1114188451560415232\",\"startTime\":\"2025-01-01\",\"endTime\":\"2025-04-12\"}", response = FixedPointResp.class)
    @RequestMapping(value = "/business/queryFixedPointList",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String queryFixedPointList(@RequestBody FixedPointRequest request){
        String result;

        FixedPointBean bean = new FixedPointBean();
        bean.setPsFpDeviceId(request.getDeviceId());
        bean.setStartTime(request.getStartTime());
        bean.setEndTime(request.getEndTime());

        try {
            List<FixedPointResp> respList = new ArrayList<>();
            for(FixedPointBean returnBean : fixedPointService.findAllToDateToDeviceId(bean)){
                FixedPointResp resp = new FixedPointResp();
                resp.setDeviceId(returnBean.getPsFpDeviceId());
                resp.setHikJson(returnBean.getPsFpHikData());
                resp.setDate(returnBean.getCreateDate());
                respList.add(resp);
            }

            Map map = new HashMap();
            map.put("data", respList);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

}
